//
//  LoveComicsCell.swift
//  Spookiness
//
//  Created by 腾億 on 2017/12/15.
//  Copyright © 2017年 tml. All rights reserved.
//

import UIKit

typealias GuessLikeTCellDidSelectClosure = (_ comic: LoveComicsModel) -> Void

class LoveComicsCell: UITableViewCell {

    fileprivate var didSelectClosure: GuessLikeTCellDidSelectClosure?
    
    fileprivate lazy var titleLB: UILabel = {
       let label = UILabel()
        label.text = "猜你喜欢"
        return label
    }()
    
    fileprivate lazy var collectionView: UICollectionView = {
       let layout = UICollectionViewFlowLayout()
        let collectionV = UICollectionView(frame: .zero, collectionViewLayout: layout)
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10)
        layout.scrollDirection = .horizontal
        collectionV.backgroundColor = self.contentView.backgroundColor
        
        collectionV.register(LoveDetailCell.self, forCellWithReuseIdentifier: "LoveDetailCell")
        collectionV.delegate = self
        collectionV.dataSource = self
        return collectionV
    }()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.addSubview(self.titleLB)
        self.contentView.addSubview(self.collectionView)
        selectionStyle = .none
        setUpSnap()
    }
    
    fileprivate func setUpSnap() {
        self.titleLB.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview().inset(UIEdgeInsetsMake(15, 15, 15, 15))
            make.height.equalTo(20)
        }
        
        self.collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(self.titleLB.snp.bottom).offset(5)
            make.left.bottom.right.equalToSuperview()
        }
    }
    
    var model: LoveModel? {
        didSet{
            self.collectionView.reloadData()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension LoveComicsCell:  UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return model?.comics.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LoveDetailCell", for: indexPath) as! LoveDetailCell
        cell.model = model?.comics[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = floor((collectionView.frame.width - 50) / 4)
        let height = collectionView.frame.height - 10
        return CGSize(width: width, height: height)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.collectionView.deselectItem(at: indexPath, animated: true)
        guard let comic = model?.comics[indexPath.row], let didSelectClosure = didSelectClosure else {
            return
        }
        didSelectClosure(comic)
    }
    
    func didSelectClosure(_ closure: @escaping GuessLikeTCellDidSelectClosure) {
        didSelectClosure = closure
    }
    
}




